var ns;
app = new Vue({
    el: '#app',
    data: {
        showsearch: false,
        showsearchmonth: false,
        showsearchparam: false,
        ProjectName:'',
        form: {
            startMonth: '', 
            activeDateDesc: '月份',
            ProjectCompanyType: 1,
            ProjectID:0,
            ProjectIDs: [],
        },
        summary: {},
        timesunmmary:{},//临时记录数量
        projectList: [], 
        XJlist:[],  ////巡检数据
        UserRankList:{
            Complate:[],//按任务完成数
            Miss:[],//按任务漏巡数
            AverageHour:[],//按平均工时
            Hour:[],//按总工时
        },
        userList:[],  ////员工工时
        formuser:{
            ID:0,
            Name:'按任务完成数',
        },
        userTypeID:0,
        userType:[
            {ID:0,Name:'按任务完成数'},
            {ID:1,Name:'按任务漏巡数'},
            {ID:2,Name:'按平均工时'},
            {ID:3,Name:'按总工时'},
        ],
        catedata:[],//日期数组
        finishdata:[],//数组
        missdata:[],
        minDate: new Date(),
        maxDate: new Date(),
        currentDate:0,
        showPopover:false,
    },
    methods: {
        formatter(type, val) {
            if (type === 'year') {
                return `${val}年`;
            }
            if (type === 'month') {
                return `${val}月`;
            }
            return val;
        },
        onConfirm(day) {
            var that = this;
            var nowdate=moment(day).format('YYYY-MM');
            that.form.startMonth = nowdate;
            that.catedata=that.getChartMonth(new Date(nowdate));
            that.doSearch();
        },
        open_task:function(){
            var that = this;
            var title='安防巡更任务列表';
            var url='patrolplan_tasklist';
            ns.openWin(url, title, {
                ProjectID: that.form.ProjectID,
                ProjectName:that.ProjectName,
                startMonth:that.form.startMonth,
                IsAnalysis:true,
            });
        },
        get_data: function () {
            var that = this;
            that.getSearchParams();
        },
        doSelectProject: function (ID,Name) {
            var that = this;
            that.ProjectName=Name;
            that.form.ProjectID=ID;
            that.form.ProjectIDs=[];
            that.form.ProjectIDs.push(ID);
            that.showsearchparam = false;
            that.doSearch();
        },
        //筛选数据
        getSearchParams: function () {
            var that = this;
            var options = {};
            options.action = "gettaskanalysisparams";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.projectList = data.projectList;
                    if(that.projectList!=undefined&&that.projectList.length>0){
                        that.doSelectProject(that.projectList[0].ID,that.projectList[0].Name); 
                    }
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        doSelectSearchParam: function (type) {
            var that = this;
            if (type == 1) { //筛选
                that.showsearchparam = !that.showsearchparam;
                that.showsearchmonth = false;
            }
            if (type == 2) { //按月
                that.showsearchparam = false;
                that.showsearchmonth = !that.showsearchmonth;
               
            }
            that.showsearch = that.showsearchparam || that.showsearchmonth;
            if (type == 2) { //按月 用的van-popup 自带了遮罩
                that.showsearch=false;
            }
        },
        doHideSearch: function () {
            var that = this;
            that.showsearchparam = false;
            that.showsearchmonth = false;
            that.showsearch = false;
            that.showPopover=false;
        },
        doSearch: function () {
            var that = this;
            that.doHideSearch();
            that.getAnalysisData();
        },
        //员工排行值改变事件
        onChangeType:function(){
            var that=this;
            that.userList=[];
            if(that.userTypeID==0){
                that.userList = that.UserRankList.Complate;//默认赋值第一个
            }
            if(that.userTypeID==1){
                that.userList = that.UserRankList.Miss;
            }
            if(that.userTypeID==2){
                that.userList = that.UserRankList.AverageHour;
            }
            if(that.userTypeID==3){
                that.userList = that.UserRankList.Hour;
            }  
        },
        getAnalysisData: function () {
            var that = this;
            //初始化
            that.XJlist=[];
            that.userList=[];
            var options = {};
            options.action = "patrolchart"; ////方法名称
            options.ProjectID = JSON.stringify(that.form.ProjectIDs);
            options.Month = that.form.startMonth;
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    if(data.status){
                        that.summary=data.info;
                        that.timesunmmary=data.KeepList;
                        that.UserRankList=that.summary.UserRankList;
                        that.userList = that.UserRankList.Complate;//默认赋值第一个
                        that.XJlist =  that.summary.MonthFinishList;
                        that.dealData(data.info);
                    }
                   
                } 
                else if(err){
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom '
                    });
                    return
                }
            }, {
                toast: true
            });
        },
        dealData:function(){
            var that=this;
            that.finishdata=[];
            that.missdata=[];
            if(that.XJlist.length>0){
                that.XJlist.forEach(item => {
                    that.finishdata.push(item.TodayFinishPoint);
                    that.missdata.push(item.TodayMissPoint);
                  });
                if( that.finishdata.length>0){
                    that.loadChart("containerzx");//加载图表
                }   
            }  
        },
        getChartMonth: function (formdate) {
            let dataTimes = [];
            let date = new Date();
            if(formdate){
                date= new Date(formdate);
            }
            let year = date.getFullYear();
            let month = date.getMonth() + 1;
            let d = new Date(year, month, 0);
            let n = +d.getDate();
            var newmonth=month<10?'0'+month:month;

            for (var i = 1; i < n + 1; i++) {
                var newi=i<10?'0'+i:i;
                dataTimes.push(newmonth+'-'+newi);
            }     
            // dataTimes.push(month+1+'-01');  
            return dataTimes;//返回天数数组
        },
        loadChart: function (IdName) {
            var that=this;
            var processcolor=['#289fff', '#ff0000'];
            var chart = new Highcharts.Chart(IdName, {
                title: {
                    text: ''
                },
                xAxis: {
                    categories: that.catedata,
                },
                yAxis: {
                    title: {
                        text: ''
                    },
                    labels: {
                        formatter: function () {
                            return this.value + '%';
                        }
                    },
                    min:0,
                    max:100,
                },
                legend: {
                    layout: 'vertical',
                    align: 'right',
                    verticalAlign: 'middle'
                },
                colors: processcolor,
                navigation: {
                    buttonOptions: {
                        enabled: false
                    }
                },
                credits: {
                    enabled: false //不显示Highcharts
                },
                tooltip: {
                    crosshairs: true,
                    shared: true,
                    valueSuffix: '%'
                },
                plotOptions: {
                    spline: {
                        marker: {
                            radius: 4,
                            lineColor: '#666666',
                            lineWidth: 1
                        }
                    },
                },
                series: [{
                    name: '完成率',
                    data: that.finishdata
                }, {
                    name: '漏巡率',
                    data: that.missdata
                }],
                responsive: {
                    rules: [{
                        condition: {
                            maxWidth: 500
                        },
                        chartOptions: {
                            legend: {
                                layout: 'horizontal',
                                align: 'center',
                                verticalAlign: 'top'
                            }
                        }
                    }]
                }
            });
        },
        onShowPopover:function(){
            this.showPopover=!this.showPopover;
        },
        //跳转临时巡查记录页面
        open_task_time:function(){
            var that = this;
            var title='临时巡查记录';
            var url='patrolplan_tasklist_record';
            ns.openWin(url, title, {
                ProjectID: that.form.ProjectID,
                ProjectName:that.ProjectName,
                startMonth:that.form.startMonth,
                IsAnalysis:true,
            });
        },
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.minDate=new Date(moment(new Date()).add(-20, 'y'));
    app.maxDate=new Date(moment(new Date()).add(50, 'y'));
    app.currentDate=new Date();
    var nowdate=moment(app.currentDate).format('YYYY-MM');
    app.form.startMonth = nowdate;
    app.catedata=app.getChartMonth(new Date(nowdate));

    app.get_data();

};
